package com.lins.framework.utils.address;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.lins.framework.R;

import java.util.ArrayList;

public class AddressSelectListAdapter
        extends RecyclerView.Adapter<AddressSelectListAdapter.mViewHolder> {

    private Context context;
    private ArrayList<AreaParsingBean> mList;
    private LayoutInflater inflater;

    public AddressSelectListAdapter(Context context, ArrayList<AreaParsingBean> mList) {
        this.context = context;
        this.mList = mList;
        this.inflater = LayoutInflater.from(context);
    }

    public void addList(ArrayList<AreaParsingBean> newList) {
        this.mList.addAll(newList);
        notifyDataSetChanged();
    }

    public void refresh(ArrayList<AreaParsingBean> newList) {
        this.mList.clear();
        this.mList.addAll(newList);
        notifyDataSetChanged();
    }

    @Override
    public AddressSelectListAdapter.mViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new mViewHolder(inflater.inflate(R.layout.app_cm_city_select_item, parent, false));
    }

    @Override
    public void onBindViewHolder(final AddressSelectListAdapter.mViewHolder holder, final int position) {
        final AreaParsingBean bean = mList.get(position);
        //TODO 优化：选择时候item的颜色改变
        holder.idTv.setText(bean.getTitle());
        if (mOnItemClickListener != null)
            holder.idTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.OnItemClick(bean.getPath(), bean.getTitle(), holder.getLayoutPosition());
                }
            });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class mViewHolder extends RecyclerView.ViewHolder {
        TextView idTv;

        public mViewHolder(View itemView) {
            super(itemView);
            idTv = (TextView) itemView.findViewById(R.id.id_tv);
        }
    }

    private OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void OnItemClick(String path, String title, int position);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
}
